// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CBL_DEFINES_H__
#define __INCLUDED_CBL_DEFINES_H__

#include "CBL_Config.h"

// Namespace defines ---------------------------------------------------------------------------------------------------

#define CB_NS								Cb

#define CB_SNS(Name)						CB_NS::Name

#define CB_NAMESPACE_START				namespace CB_NS {
#define CB_NAMESPACE_END					}

#define CB_SUBNAMESPACE_START(Name)		namespace CB_NS { namespace Name {
#define CB_SUBNAMESPACE_END					} }

#ifdef _DEBUG

#define CB_DEBUG

#endif

#ifdef _WIN32
// MSC -----------------------------------------------------------------------------------------------------------------

#define CB_MSC
#define CB_WINDOWS
#define WIN32_LEAN_AND_MEAN
#define _CRT_SECURE_NO_WARNINGS

#define CB_DIRECTORY_SEPARATOR '\\'

// Byte algin for SIMD Types
#define CB_DECL_ALIGN(_Bytes) __declspec(align(_Bytes))

// Cross compiler forceinline
#define CB_FORCEINLINE __forceinline
#define CB_INLINE_ATTR

// Cross compiler assembler
#define CB_ASM __asm
#define CB_ASM_pushfl pushfd
#define CB_ASM_popfl popfd

// utility
#define CB_ATOI64(x) _atoi64(x)

#else
// GCC -----------------------------------------------------------------------------------------------------------------

#define CB_GCC

#ifdef __APPLE__
#define CB_MACOSX
#else
#error "Cherubim does currently only work on WIN32 (MSC) or MACOSX (GCC). Make sure platform macros are defined"
#endif

#define CB_DIRECTORY_SEPARATOR '/'

// Byte algin for SIMD Types
#define CB_DECL_ALIGN(_Bytes) __attribute__((aligned(_Bytes)))

// Cross compiler forceinline
#define CB_FORCEINLINE inline
#define CB_INLINE_ATTR __attribute__((always_inline))

// Cross compiler assembler
#define CB_ASM asm
#define CB_ASM_pushfl pushfl
#define CB_ASM_popfl popfl

// utility
#define CB_ATOI64(x) atoll(x)

#endif

// DLL defines ---------------------------------------------------------------------------------------------------

#ifdef CB_MSC

#ifdef CBL_LINK_DYNAMIC
	#ifdef CBL_COMPILE
		#define BASELIB_API __declspec(dllexport)
	#else
		#define BASELIB_API __declspec(dllimport)
	#endif

	#ifdef _WINDLL
		#define DERIVED_API __declspec(dllexport)
	#else
		#define DERIVED_API __declspec(dllimport)
	#endif
#else
	#define BASELIB_API
	#define DERIVED_API
#endif

#else

#ifdef CBL_LINK_DYNAMIC
	#ifdef CBL_COMPILE
		#define BASELIB_API __attribute__ ((visibility("default")))
	#else
		#define BASELIB_API
	#endif

	#define DERIVED_API
#else
	#define BASELIB_API
	#define DERIVED_API
#endif

#endif

// Architecture defines ------------------------------------------------------------------------------------------------

#if defined(__i386__) || defined(_M_IX86)
#define CB_ARCH_X86
#define ASM_BREAKPOINT int 3

#elif defined(__POWERPC__)
#define CB_ARCH_PPC
#define ASM_BREAKPOINT \
	li r0, 20;\
    sc;\
	nop;\
	li r0, 37;\
	li r4, 2;\
	sc;\
	nop

#elif defined(CBL_COMPILE)
#error "Unsupported CPU architecture. Maybe __i386__, _M_IX86 or __POWERPC__ not set?"
#endif

// Basic types ---------------------------------------------------------------------------------------------------------

typedef unsigned char		uint8;
typedef char				int8;

typedef unsigned short		uint16;
typedef short				int16;

typedef unsigned int		uint32;
typedef int					int32;

#ifdef CB_MSC
typedef unsigned __int64	uint64;
typedef __int64				int64;
#else
typedef unsigned long long	uint64;
typedef long long			int64;
#endif

#ifdef CB_64BIT
typedef uint64				memint;
#else
typedef uint32				memint;
#endif

enum EInitType
{
    Uninitialized = 0
};

// Tool defines --------------------------------------------------------------------------------------------------------

#define CB_UNUSED(Name)

#undef NULL
#define NULL 0

#endif